﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Diagnostics;
using System.ComponentModel;

namespace IronCow.Posh.Commands
{
    [Cmdlet(VerbsCommon.Get, "RtmAuthenticationUrl")]
    [System.ComponentModel.Description("Run a web browser so that the user can authenticate with RTM.")]
    public class GetRtmAuthenticationUrlCommand : RtmCmdlet
    {
        [Parameter(
            HelpMessage = "The level of permissions to request.")]
        [DefaultValue(AuthenticationPermissions.Delete)]
        public AuthenticationPermissions Permissions { get; set; }

        [Parameter(
            HelpMessage = "Whether to automatically launch a web browser with the authentication URL.")]
        [Alias("Start")]
        [DefaultValue(false)]
        public SwitchParameter StartUrl { get; set; }

        public GetRtmAuthenticationUrlCommand()
        {
            Permissions = AuthenticationPermissions.Delete;
            StartUrl = false;
        }

        protected override void ProcessRecord()
        {
            string frob = Rtm.GetFrob();
            string url = Rtm.GetAuthenticationUrl(frob, AuthenticationPermissions.Delete);
            WriteObject(new { Frob = frob, Url = url });

            if (StartUrl)
                Process.Start(url);
        }
    }
}
